import time
import requests
from perf_settings import *
from common.hande_device import HandleDevices
from influxdb_client import Point

from common.handle_influxdb import InfluxDBHandler

do_devices = HandleDevices(pkgName=MID_pkgName)
do_influxdb = InfluxDBHandler()

apm_pid_main, apm_server_main = do_devices.handle_main_device(deviceId_001=deviceId_001)

while True:
    # 准备数据点列表
    try:
        time.sleep(2)
        device_001_main_cpu = do_devices.get_cpu(apm_pid_main)["appCpuRate"]
        device_001_main_mem = do_devices.get_mem(apm_pid_main)
        device_001_server_cpu = do_devices.get_cpu(apm_server_main)["appCpuRate"]
        device_001_server_mem = do_devices.get_mem(apm_server_main)

        measurements = [
            Point("device_001").tag("pid", "main_pid").field("cpu", device_001_main_cpu),
            Point("device_001").tag("pid", "main_pid").field("mem", device_001_main_mem),
            Point("device_001").tag("pid", "main_server").field("cpu", device_001_server_cpu),
            Point("device_001").tag("pid", "main_server").field("mem", device_001_server_mem),

        ]
        do_influxdb.write_mul_data(measurements=measurements)
    except:
        try:
            requests.get(url="https://api.day.app/s3KoLFVvWVWdYiiro2gKW/多屏极氪性能采集可能出现问题")
        except:
            pass
